package com.dxl.system.controller;

import com.dxl.base.Result;
import com.dxl.base.Results;
import com.dxl.system.model.Label;
import com.dxl.system.model.Note;
import com.dxl.system.service.LabelService;
import com.dxl.system.service.NoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <b> 标签控制器
 * </b><br><br><i>Description</i> :
 * <br>Date: 2019-09-25    <br>Author : dxl
 */
@Api(value = "标签接口",tags = {"标签--接口"})
@Controller
@RequestMapping("/label")
public class LabelController {

    @Autowired(required = false)
    private LabelService labelService;
    @Autowired(required = false)
    private NoteService noteService;


    /**
     * <b> 插入标签
     * </b><br><br><i>Description</i> : 不能为空：标签名称-label_name ；不能重复：标签名称-label_name  
     * @param label Label
     * @return com.dxl.base.Result Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "插入标签",notes = "不能为空：标签名称-label_name ；不能重复：标签名称-label_name  ")
    @RequestMapping(value = "/addOne",method = {RequestMethod.POST })
    @ResponseBody
    public Result addOne(@Valid @RequestBody Label label) {
        Result result = labelService.addOne(label);
        return result;
    }

    /**
     * <b> 插入标签Html
     * </b><br><br><i>Description</i> : 不能为空：标签名称-label_name ；不能重复：标签名称-label_name
     * @return String
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "插入标签Html",notes = "不能为空：标签名称-label_name ；不能重复：标签名称-label_name  ")
    @RequestMapping(value = "/addOneHtml",method = {RequestMethod.GET })
    public String addOneHtml() {
        return "label/label_add";
    }

    /**
     * <b> 分配文件给标签
     * </b><br><br><i>Description</i> :
     * @param labelId Integer，noteIds -List: Integer
     * @return com.dxl.base.Result Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "分配文件给标签",notes = "无注意点！")
    @RequestMapping(value = "/addNoteToLabel",method = {RequestMethod.POST })
    @ResponseBody
    public Result addNoteToLabel(@RequestParam(value="labelId", required = true) Integer labelId, @RequestParam(value="noteIds", required = true) List<Integer> noteIds){
        Result result = labelService.addNoteToLabel(labelId, noteIds);
        return result;
    }

    /**
     * <b> 删除标签
     * </b><br><br><i>Description</i> : 
     * @param labelIds List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "删除标签",notes = "无注意点！")
    @RequestMapping(value = "/delByIds",method = {RequestMethod.POST })
    @ResponseBody
    public Result delByIds(@RequestParam(value="labelIds", required = true) List<Integer> labelIds){
        Result result = labelService.delByIds(labelIds);
        return result;
    }

    /**
     * <b> 删除标签及文件
     * </b><br><br><i>Description</i> :
     * @param label Label
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "删除标签及文件",notes = "无注意点！")
    @RequestMapping(value = "/delJoinByIds",method = {RequestMethod.POST })
    @ResponseBody
    @Transactional
    public Result delJoinByIds(@Valid @RequestBody Label label){
        Label label2 = new Label();
        label2.setLabelName(label.getLabelName());
        List<Label> labelList = (List<Label>) labelService.getList(label2).getData();
        List<Integer> labelIds = new ArrayList<>();
        labelIds.add(labelList.get(0).getId());
        Result result = labelService.delByIds(labelIds);
        List<Note> noteList = (List<Note>) labelService.getAllNoteInLabel(labelList.get(0).getId()).getData();
        List<Integer> noteIds = noteList.stream().map(Note::getId).collect(Collectors.toList());
        delAllNoteFromLabel(labelList.get(0).getId());
        return result;
    }

    /**
     * <b> 移除标签中文件
     * </b><br><br><i>Description</i> :
     * @param labelId Integer，noteIds -List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "移除标签中文件",notes = "无注意点！")
    @RequestMapping(value = "/delNoteFromLabel",method = {RequestMethod.POST })
    @ResponseBody
    public Result delNoteFromLabel(@RequestParam(value="labelId", required = true) Integer labelId, @RequestParam(value="noteIds", required = true) List<Integer> noteIds) {
Result result = labelService.delNoteFromLabel(labelId, noteIds);
        return result;
    }

    /**
     * <b> 移除标签中全部文件
     * </b><br><br><i>Description</i> :
     * @param labelId Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "移除标签中全部文件",notes = "无注意点！")
    @RequestMapping(value = "/delAllNoteFromLabel",method = {RequestMethod.POST })
    @ResponseBody
    public Result delAllNoteFromLabel(@RequestParam(value="labelId", required = true) Integer labelId) {
Result result = labelService.delAllNoteFromLabel(labelId);
        return result;
    }

    /**
     * <b> 编辑标签
     * </b><br><br><i>Description</i> : 不能重复：标签名称-label_name 
     * @param label Label
     * @return com.dxl.base.Result Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "编辑标签",notes = "不能重复：标签名称-label_name ")
    @RequestMapping(value = "/editOne",method = {RequestMethod.POST })
    @ResponseBody
    public Result editOne(@Valid @RequestBody Label label) {
        Result result = labelService.editOne(label);
        return result;
    }

    /**
     * <b> 编辑标签Html
     * </b><br><br><i>Description</i> : 不能重复：标签名称-label_name
     * @param labelName String
     * @return String
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "编辑标签",notes = "不能重复：标签名称-label_name ")
    @RequestMapping(value = "/editOneHtml",method = {RequestMethod.GET })
    public String editOneHtml(@RequestParam(value="labelName", required = true) String labelName,Model model) {
        Label label2 = new Label();
        label2.setLabelName(labelName);
        List<Label> labelList = (List<Label>) labelService.getList(label2).getData();
        model.addAttribute(labelList.get(0));
        return "label/label_edit";
    }

    /**
     * <b> ID查询标签
     * </b><br><br><i>Description</i> : 
     * @param labelId Integer
     * @return com.dxl.base.Result: Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "ID查询标签",notes = "无注意点！")
    @RequestMapping(value = "/getById",method = {RequestMethod.POST })
    @ResponseBody
    public Result getById(@RequestParam(value="labelId", required = true) Integer labelId) {
        Result result = labelService.getById(labelId);
        return result;
    }

    /**
     * <b> 条件查询标签
     * </b><br><br><i>Description</i> : 
     * @param label Label
     * @return com.dxl.base.Result List: Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "条件查询标签",notes = "无注意点！")
    @RequestMapping(value = "/getList",method = {RequestMethod.POST })
    @ResponseBody
    public Result getList(@Valid @RequestBody Label label) {
        Result result = labelService.getList(label);
        return result;
    }

    /**
     * <b> 分页条件查询标签
     * </b><br><br><i>Description</i> : 
     * @param label Label, currentPage -Integer
     * @return com.dxl.base.Result List: Label
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "分页条件查询标签",notes = "无注意点！")
    @RequestMapping(value = "/getListByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getListByPage(@Valid @RequestBody Label label, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "40") Integer pageSize) {
        Result result = labelService.getListByPage(label, currentPage, pageSize);
        return result;
    }

    /**
     * <b> 查询标签中全部文件
     * </b><br><br><i>Description</i> : 联表
     * @param labelId Integer
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "查询标签中全部文件",notes = "无注意点！")
    @RequestMapping(value = "/getAllNoteInLabel",method = {RequestMethod.POST })
    @ResponseBody
    public Result getAllNoteInLabel(@RequestParam(value="labelId", required = true) Integer labelId) {
        Result result = labelService.getAllNoteInLabel(labelId);
        return result;
    }

    /**
     * <b> 分页查询标签中全部文件
     * </b><br><br><i>Description</i> : 联表
     * @param labelId Integer, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "分页查询标签中全部文件",notes = "无注意点！")
    @RequestMapping(value = "/getAllNoteInLabelByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getAllNoteInLabelByPage(@RequestParam(value="labelId", required = true) Integer labelId, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize) {
        Result result = labelService.getAllNoteInLabelByPage(labelId, currentPage, pageSize);
        return result;
    }

    /**
     * <b> 分页查询标签中全部文件
     * </b><br><br><i>Description</i> : 联表
     * @param label Label, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "分页查询标签中全部文件",notes = "无注意点！")
    @RequestMapping(value = "/getAllNoteInLabelByNameByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getAllNoteInLabelByNameByPage(@Valid @RequestBody Label label, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize) {
        Label label1 = new Label();
        label1.setLabelName(label.getLabelName());
        List<Label> labelList = (List<Label>) labelService.getList(label1).getData();
        if(labelList == null || labelList.size() < 1){
            return Results.failure("无该标签:(");
        }
        Result result = labelService.getAllNoteInLabelByPage(labelList.get(0).getId(), currentPage, pageSize);
        return result;
    }

    /**
     * <b>  模糊查询标签
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：label_name-标签名称 
     * @param label Label
     * @return com.dxl.base.Result List: Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @ApiOperation(value = "模糊查询标签",notes = "模糊查询；模糊字段(模式：单字段)：全匹配：label_name-标签名称 ")
    @RequestMapping(value = "/getLikeLabel",method = {RequestMethod.POST })
    @ResponseBody
    public Result getLikeLabel(@Valid @RequestBody Label label){
        Result result = labelService.getLikeLabel(label);
        return result;
    }

    /**
     * <b> 分页模糊查询标签
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：label_name-标签名称 
     * @param label Label, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Label
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "分页模糊查询标签",notes = "模糊查询；模糊字段(模式：单字段)：全匹配：label_name-标签名称 ")
    @RequestMapping(value = "/getLikeLabelByPage",method = {RequestMethod.POST })
    @ResponseBody
    public Result getLikeLabelByPage(@Valid @RequestBody Label label, @RequestParam(value="currentPage", required = false, defaultValue = "1") Integer currentPage, @RequestParam(value="pageSize", required = false, defaultValue = "20") Integer pageSize){
        Result result = labelService.getLikeLabelByPage(label, currentPage, pageSize);
        return result;
    }

    /**
     * <b> 标签能否新增
     * </b><br><br><i>Description</i> : 无权限限制；不能为空：标签名称-label_name ；不能重复：标签名称-label_name  
     * @param label Label
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "标签能否新增",notes = "无权限限制；不能为空：标签名称-label_name ；不能重复：标签名称-label_name  ")
    @RequestMapping(value = "/isAddable",method = {RequestMethod.POST })
    @ResponseBody
    public Result isAddable(@Valid @RequestBody Label label) {
        Result result = labelService.isLabelAddable(label);
        return result;
    }

    /**
     * <b> 标签能否编辑
     * </b><br><br><i>Description</i> : 无权限限制；不能重复：标签名称-label_name 
     * @param label Label
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @ApiOperation(value = "标签能否编辑",notes = "无权限限制；不能重复：标签名称-label_name ")
    @RequestMapping(value = "/isEditable",method = {RequestMethod.POST })
    @ResponseBody
    public Result isEditable(@Valid @RequestBody Label label) {
        Result result = labelService.isLabelEditable(label);
        return result;
    }
}
